package cn.edu.gcu.giftems.service;

import cn.edu.gcu.giftems.daos.UserDao;
import cn.edu.gcu.giftems.pojos.User;
import cn.edu.gcu.giftems.utils.PageUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-24
 * @see
 */
@Transactional
public class UserService {

    private UserDao userDao = new UserDao();

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void save(User user) {
        userDao.save(user);
    }

    //根据用户编号查询用户信息
    public User findById(int id) {
        return userDao.finById(id);
    }

    //更新用户信息
    public void update(User user) {
        userDao.update(user);
    }

    //根据用户id删除用户
    public void delete(User user) {
        userDao.delete(user);
    }

    public int findCount() {
        return userDao.findCount();
    }

    public User login(User user) {
        User existUser = userDao.findByUsernameAndPassword(user);
        return existUser;
    }

    //分页查询
    public PageUtils<User> findByPage(int currPage) {
        PageUtils<User> PageUtils = new PageUtils<User>();
        //封装当前的页数
        PageUtils.setCurrPage(currPage);
        //封装每页记录数
        int pageSize = 10;
        PageUtils.setPageSize(pageSize);
        //封装总记录数
        int totalCount = userDao.findCount();
        PageUtils.setTotalCount(totalCount);
        //封装总页数
        int totalPage = totalCount%pageSize == 0?totalCount/pageSize:(totalCount/pageSize+1);
        PageUtils.setTotalPage(totalPage);
        //封装每页显示的数据
        int begin = (currPage - 1)*pageSize;
        List<User> list = userDao.findByPage(begin,pageSize);
        PageUtils.setList(list);
        return PageUtils;
    }

    public List<User> findAll() {
        return userDao.findAll();
    }

    public User findPassword(User user) {
        User existUser = userDao.findPassword(user);
        return existUser;
    }

    public PageUtils<User> findByParameter(int currPage, String filter_name, String simple_filter_value) {
        PageUtils<User> PageUtils = new PageUtils<User>();
        PageUtils.setCurrPage(currPage);
        PageUtils.setTotalPage(1);
        List<User> list = userDao.findByParameter(filter_name,simple_filter_value);
        PageUtils.setList(list);
        int totalCount = list.size();
        PageUtils.setTotalCount(totalCount);
        return PageUtils;
    }
}
